package com.qasystem.qa.system.controller;

import com.qasystem.qa.core.base.BaseEnum;
import com.qasystem.qa.core.base.Result;
import com.qasystem.qa.core.constants.BaseEnums;
import com.qasystem.qa.core.utils.Results;
import com.qasystem.qa.system.entity.Question;
import com.qasystem.qa.system.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/public")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @GetMapping("/questions")
    @ResponseBody
    public Result getQuestions() {
        List<Question> questionList = questionService.selectAll();
        Result  result = null;
        if (questionList != null ) {
            result = Results.successWithData(questionList, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
        }
        else {
            result = Results.failure(BaseEnums.FAILURE.code(), BaseEnums.FAILURE.desc());
        }
        return result;
    }
    @GetMapping("/question/{id}")
    @ResponseBody
    public Result getQuestion (@PathVariable String id) {
        System.out.println("id: " + id);
        Question question = null;
        if (id != null)
            question = questionService.get(id);
        //List<Question> questions = questionService.select(id+",");
        Result result = null;
        if (question != null) {
            result = Results.successWithData(question, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
        }
        else
            result = Results.failure();
        return result;
    }


}
